﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 沙驰评审会预投打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 沙驰评审会预投打印()
        {
            InitializeComponent();
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                string sqldm = @"exec zdy_getInfoByDm '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtdm = DBHelper2.ExecuteDataTable(sqldm);
                if (dtdm.Rows.Count > 0)
                {
                    NewUsb.FindUSBPrinter();
                    for (int k = 0; k < NewUsb.USBPortCount; k++)
                    {
                        if (NewUsb.LinkUSB(k))
                        {
                            SendData2USB("SIZE 60 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                            SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                            SendData2USB("CLS\r\n");//清空缓冲区
                            SendData2USB("DENSITY 7\r\n");//打印浓度
                            SendData2USB("REFERENCE 0,0\r\n");

                            SendData2USB("TEXT 10,10,\"TSS24.BF2\",0,1,1,\"品类\"\r\n");
                            SendData2USB("TEXT 100,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["productname"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 270,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["xilie"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 350,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["zhuti"].ToString() + "\"\r\n");


                            SendData2USB("TEXT 10,45,\"TSS24.BF2\",0,1,1,\"短码\"\r\n");
                            SendData2USB("TEXT 100,45,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["dm"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,80,\"TSS24.BF2\",0,1,1,\"价格\"\r\n");
                            SendData2USB("TEXT 100,80,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["price"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 200,80,\"TSS24.BF2\",0,1,1,\"版型\"\r\n");
                            SendData2USB("TEXT 300,80,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["banxing"].ToString() + "\"\r\n");


                            //预投明细
                            string sqldm_zly = @"exec [zdy_getInfoByDm_yt] '{0}'";
                            sqldm_zly = string.Format(sqldm_zly, dm);
                            DataTable dtdm_zly = DBHelper2.ExecuteDataTable(sqldm_zly);
                            int gd = 85;
                            for (int x = 0; x < dtdm_zly.Rows.Count; x++)
                            {
                                gd = gd + 30;
                                SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_zly.Rows[x]["username"].ToString() + "\"\r\n");
                                SendData2USB("TEXT 200," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdm_zly.Rows[x]["xdl"].ToString() + "\"\r\n");
                            }
                            gd = gd + 20;
                            SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"------------------------------------\"\r\n");
                            //预投汇总明细
                            string sqldmpjf = @"exec [zdy_getInfoByDm_ytsum] '{0}'";
                            sqldmpjf = string.Format(sqldmpjf, dm);
                            DataTable dtdmpjf = DBHelper2.ExecuteDataTable(sqldmpjf);

                            for (int j = 0; j < dtdmpjf.Rows.Count; j++)
                            {
                                gd = gd + 30;
                                SendData2USB("TEXT 10," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[j]["lx"].ToString() + "\"\r\n");
                                SendData2USB("TEXT 200," + gd + ",\"TSS24.BF2\",0,1,1,\"" + dtdmpjf.Rows[j]["xdl"].ToString() + "\"\r\n");
                            }

                            SendData2USB("PRINT 1\r\n");
                            NewUsb.CloseUSBPort();

                            textBox1.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("请确定是否连接上打印机");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("短码不正确");
                }
            }
        }
    }
}
